<template>
  <div class="login">
    <div class="box">
      <h1>后台管理</h1>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormrules">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            show-password
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="goLogin">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { LoginApi } from "@/http/api";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginFormrules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 5,
            message: "用户名必须在 3 到 5 个字符",
            trigger: "blur",
          },
        ],

        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "密码要在 3 到 6 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    goLogin() {
      console.log("login");
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return false;
        //开始调用后台登录接口
        const res = await LoginApi(this.loginForm);
        console.log("是否登录成功：", res);
        this.$router.push("/home");
      });
    },
  },
  components: {},
};
</script>

<style lang='scss' scoped>
.login {
  width: 100%;
  height: 100vh;
  background-color: #525a62;
}
.box {
  background-color: #fff;
  padding: 20px;
  width: 500px;
  height: 240px;
  // border: 1px solid red;
  // box-shadow: 1px 2px 2px ;
  text-align: center;
  position: fixed;
  top: 30%;
  left: 30%;
}
</style>
